﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Economo.Services.DataContext;
using Economo.Services.EntitiesConversion;

namespace Economo.Services.BusinessLogic
{
    public class AccountsBusinessLogic
    {
        public BusinessEntities.Account GetAccountById(Int16 id)
        {
            AccountsDataContext da = new AccountsDataContext();
            try
            {
                DataContext.Account Account = da.GetAccountById(id);
                BusinessEntities.Account result = Convertions.ConvertToBusinessEntity<BusinessEntities.Account>(Account);
                return result;
            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageBusinessLogicException(ex);
                return null;
            }
            finally
            {
                da.Dispose();
            }

        }

        public Int16 InsertAccount(BusinessEntities.Account newAccount)
        {
            AccountsDataContext da = new AccountsDataContext();
            try
            {
                Account Account = Convertions.ConvertToDataEntity<Account>(newAccount);
                newAccount.Id = da.InsertAccount(Account);
                return newAccount.Id;
            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageBusinessLogicException(ex);
                return 0;
            }
            finally
            {
                da.Dispose();
            }
        }

        public void UpdateAccount(BusinessEntities.Account updatedAccount)
        {
            AccountsDataContext da = new AccountsDataContext();
            try
            {
                Account Account = Convertions.ConvertToDataEntity<Account>(updatedAccount);
                da.UpdateAccount(Account);
            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageBusinessLogicException(ex);
            }
            finally
            {
                da.Dispose();
            }
        }

        public void DeleteAccount(byte id)
        {
            AccountsDataContext da = new AccountsDataContext();
            try
            {
                da.DeleteAccount(id);
            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageBusinessLogicException(ex);
            }
            finally
            {
                da.Dispose();
            }

        }
    }
}
